#pragma once
#include <stdio.h>
#include<iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string>
#include <cstdlib>
#include <unistd.h>
#define PATHNAME "."
#define PROJ_ID 0x6666

int shmHelper(int size, int flags)
{
    key_t _key = ftok(PATHNAME, PROJ_ID);
    std::cout<<_key<<std::endl;
    if (_key < 0)
    {
        perror("ftok");
        return -1;
    }
    int shmid = 0;
    if ((shmid = shmget(_key, size, flags)) < 0)
    {
        perror("shmget");
        return -2;
    }
    return shmid;
}

int shmCreate(int size)
{
    return shmHelper(size, IPC_CREAT | IPC_EXCL|0666);
}

int shmGet(int size)
{
    return shmHelper(size, IPC_CREAT);
}

int destroyShm(int shmid)
{
    if (shmctl(shmid, IPC_RMID, nullptr) < 0)
    {
        perror("shmctl");
        return -1;
    }
    return 0;
}
